package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.common.util.WeatherProvinceUtil;
import com.gable.report.model.ChartsAreaQuery;
import com.gable.report.model.ChartsCityWeatherView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForAreaOverviewService;
import com.gable.report.util.ChartsUtil;
import com.gable.report.util.HttpProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: 杨青桦
 * @Description:  区报表运营管理
 * @Date: 2018/1/12 10:58
 */
@Controller
@RequestMapping("charts/area/overview")
public class ChartsForAreaOverviewController {

    @Resource
    HttpServletRequest request;

    @Autowired
    ChartsForAreaOverviewService overviewService;

    /**
     * @Author: 杨青桦
     * @Description:  区报表运营管理界面
     * @Date: 2018/8/14 15:20
     */
    @RequestMapping(value="/page", method= RequestMethod.GET)
    public String page(){
        User user = (User)request.getSession().getAttribute("loginUser");
        if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
            return "charts-authority";
        }

        return "charts/area/overview";
    }



   /**
    * @Author: 杨青桦
    * @Description: 获取所在区域天气
    * @Date: 2018/1/23 11:33
    */
    @RequestMapping(value="/weather", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryWeather(){

        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Long areaId = ChartsUtil.userRegionMap.get(user.getId());
            Map<String, Object> map = overviewService.queryAreaRegion(areaId);
            String provinceName = (String)map.get("provinceName");
            String ChinaXml = HttpProcessUtil.getXml("china"); //获取全国各省会的天气
            if(StringUtils.isEmpty(provinceName)){ //没有省份信息，直接返回北京市天气
               return new ReturnResultUtil(1,"查询成功",HttpProcessUtil.getCityWeather(ChinaXml,"北京"));
            }else{
                //获取省份的拼音及省份各地级市的天气
                String pinyin = WeatherProvinceUtil.getValue(provinceName);
                String provinceXml = HttpProcessUtil.getXml(pinyin);
                String cityName = (String)map.get("cityName");
                if("市辖区".equals(cityName)){
                    cityName = ((String)map.get("areaName")).replace("区","");
                }else{
                    cityName = cityName.replace("市","");
                }
                //获取区和市的天气，获取不到直接为省会的天气
                ChartsCityWeatherView ccwv = HttpProcessUtil.getCityWeather(provinceXml,cityName);
                if(ccwv == null){
                    ccwv =   HttpProcessUtil.getProvincialCapitalWeather(ChinaXml,WeatherProvinceUtil.getValue(provinceName));
                }
                return new ReturnResultUtil(1,"查询成功",ccwv);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 医院等级情况
     * @Date: 2018/8/14 20:49
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/level", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryWeather(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalLevels(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  类型和风险等级金额分布
     * @Date: 2018/8/14 20:50
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/type-risk", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsTypeAndRisk(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryAssetsTypeAndRisk(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 医院服务人次列表接口(柱状折线图,排名,更多)
     * @Date: 2018/8/15 15:07
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/service-list", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalServicePeopleList(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalServicePeopleList(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description: 服务人次饼状图
     * @Date: 2018/8/15 15:07
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/service-pie", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryServicePeoplePie(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryServicePeoplePie(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  医院地图坐标
     * @Date: 2018/8/16 10:12
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/map", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalMap(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalMap(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 医院维修详情
     * @Date: 2018/8/15 15:07
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/repair", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalRepair(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalRepair(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }


    /**
     * @Author: 杨青桦
     * @Description:  医院保养详情
     * @Date: 2018/8/17 14:44
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/upkeep", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalUpkeep(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalUpkeep(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    /**
     * @Author: 杨青桦
     * @Description:  医院强检详情
     * @Date: 2018/8/17 14:44
     * @param chartsAreaQuery
     * @return
     */
    @RequestMapping(value="/compulsory", method= RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryHospitalCompulsory(ChartsAreaQuery chartsAreaQuery){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getGovernmentType() == null || user.getGovernmentType() < 4){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Map returnMap = overviewService.queryHospitalCompulsory(user,chartsAreaQuery);
            return new ReturnResultUtil(1,"查询成功",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

}
